const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
const router = new Router();
app.use(bodyParser());


router.post('/api', async (ctx, next) => {
	try{
		await next();
		ctx.response.body = ctx.request.body;
	}catch(e){
		console.log(e);
		//TODO handle the exception
	}
})
router.get('/hello', (ctx, next) => {
	ctx.body = 'Hello world';
})

router.get('/hello/:name', (ctx, next) => {
	let name = ctx.params.name;
	ctx.response.body = `Hello, ${name}!`; 
})

app.use(async (ctx, next) => {
	console.log(`Time: ${Date.now()}`);
	await next();
})

app.use(router.routes());

app.listen(3000, () => {
	console.log('server is running at 3000 port: ')
});